<?php
# Demo for using the RAMS PHP Client Library (based on the Zend GData libraries).
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_once './RawsClient/Raws/Rams.php';
define('BASE', 'http://rams.mon01.rambla.be/');

# Provide your own credentials here
define('USER', 'xxx');
define('PWD', 'xxx');

try {
  $rams = new Rams(USER, PWD, BASE);

  # TRAFFIC TEST
  # ------------
  echo "->Retrieving a RAMS Traffic Feed:\n";
  # Pass a Query-String to limit the feed elements to files only
  $query = new Rams_TrafficQuery("http");
  $query->setMonth("all");
  $query->setYear("2008");
  // $query->setDay("curr");
  // $query->setHour("-23");
  // $query->setWeek("26-");
  # get the feed
  $feed = $rams->getTrafficFeed("/", $query);
  foreach ($feed as $entry) {
    # Retrieve the entry element's "kind" and "path" attributes
    echo "Found " . $entry->type . " entry with kind = " . $entry->kind . "\n";
    # Retrieve the value of the params child elements
    echo "Path: " . $entry->content->params->path->text . "\n";
    echo "Volume: " . $entry->content->params->volume->text . "\n";
    echo "Hits: " . $entry->content->params->hits->text . "\n";
    echo "Updated: " . $entry->content->params->updated->text . "\n";
    
    echo "Pct 0_10: " . $entry->content->params->pct_0_10->text . "\n";
    echo "Pct 10_20: " . $entry->content->params->pct_10_20->text . "\n";
    echo "Pct 20_30: " . $entry->content->params->pct_20_30->text . "\n";
    echo "Pct 30_40: " . $entry->content->params->pct_30_40->text . "\n";
    echo "Pct 40_50: " . $entry->content->params->pct_40_50->text . "\n";
    echo "Pct 50_60: " . $entry->content->params->pct_50_60->text . "\n";
    echo "Pct 60_70: " . $entry->content->params->pct_60_70->text . "\n";
    echo "Pct 70_80: " . $entry->content->params->pct_70_80->text . "\n";
    echo "Pct 80_90: " . $entry->content->params->pct_80_90->text . "\n";
    echo "Pct 90_100: " . $entry->content->params->pct_90_100->text . "\n";
  }
  
  # STORAGE TEST
  # ------------
  echo "\n->Retrieving a RAMS Storage Feed:\n";
  # Pass a Query-String to limit the feed elements to files only
  $query = new Rams_StorageQuery();
  $query->setMonth("all");
  $query->setYear("2008");
  # get the feed
  $feed = $rams->getStorageFeed("/", $query);
  foreach ($feed as $entry) {
    # Retrieve the entry element's "month" and "year" attributes
    echo "Found entry with year = " . $entry->year . " and month = " . $entry->month . "\n";
    # Retrieve the value of the params child elements
    echo "Volume: " . $entry->content->params->volume->text . "\n";
    echo "Location: " . $entry->content->params->location->text . "\n";
    echo "Updated: " . $entry->content->params->updated->text . "\n";
  }
   
  # FILTER TEST
  # ------------
  echo "\n->Retrieving a RAMS Filter Feed:\n";
  # Pass a Query-String to limit the feed elements to files only
  $query = new Rams_FilterQuery();
  $query->setType("all");
  $query->setActive("1");
  # get the feed
  $feed = $rams->getFilterFeed("/", $query);
  foreach ($feed as $entry) {
    # Retrieve the entry element's "month" and "year" attributes
    echo "Found entry with type = " . $entry->type . " and active = " . $entry->active . "\n";
    # Retrieve the value of the params child elements
    echo "Name: " . $entry->content->params->name->text . "\n";
    echo "Filter: " . $entry->content->params->filter->text . "\n";
    echo "Operator: " . $entry->content->params->operator->text . "\n";
    echo "Threshold: " . $entry->content->params->threshold->text . "\n";
    echo "Last Threshold Warning: " . $entry->content->params->last_threshold_warning->text . "\n";
    echo "Email: " . $entry->content->params->email->text . "\n";
  }
 
  
}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Raws::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
